package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.entity.*;
import com.zhejiangzhengyuan.municipal_common.entity.dto.*;
import com.zhejiangzhengyuan.municipal_common.entity.vo.InspectionTasksVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.OneWayCaseNoHandVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.TaskVo;
import com.zhejiangzhengyuan.municipal_common.mapper.*;
import com.zhejiangzhengyuan.municipal_common.service.CaseManagementService;
import com.zhejiangzhengyuan.municipal_common.service.CaseService;
import com.zhejiangzhengyuan.municipal_common.service.InspectionRecordService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil;
import com.zhejiangzhengyuan.municipal_common.utils.FileDuplicateCode;
import com.zhejiangzhengyuan.municipal_common.utils.JwtUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.FileUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;
import static com.zhejiangzhengyuan.municipal_common.utils.JwtUtil.HEADER_STRING;
import static com.zhejiangzhengyuan.municipal_common.utils.StaticConstants.WAY_MODULE;
import static com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.FileUtil.getFilePo;
import static com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.FileUtil.getUrlsByRequest;

/**
 * 巡检记录表，用于记录目前巡检工作产生数据 服务实现类
 *
 * @author linju
 * @since 2019-11-22
 */
@Slf4j
@Service
@Transactional
public class InspectionRecordServiceImpl
    extends ServiceImpl<InspectionRecordMapper, InspectionRecord>
    implements InspectionRecordService {

  private String caseType = "巡检案件";
  @Autowired private InspectionRecordMapper inspectionRecordMapper;
  @Autowired private FileMapper fileMapper;
  @Autowired private InspectionRecordFileMappingMapper inspectionRecordFileMappingMapper;
  @Autowired private BridgeGisMapper bridgeGisMapper;
  @Autowired private RoadGisMapper roadGisMapper;
  @Autowired private InspectionTasksMapper inspectionTasksMapper;
  @Autowired private FileDuplicateCode fileDuplicateCode;
  @Autowired private InspectionRecordQueryMapper inspectionRecordQueryMapper;
  @Autowired private InspectionDtoServiceImpl inspectionDtoService;
  @Autowired private CaseManagementService caseManagementService;
  @Autowired private CaseService caseService;
  @Autowired private HttpServletRequest request;
  @Autowired private DepartmentMapper departmentMapper;
  @Autowired private CaseInspectionEventMappingMapper caseInspectionEventMappingMapper;
  @Autowired private CaseMapper caseMapper;
  @Autowired private InspectionEventMapper inspectionEventMapper;
  @Autowired private InspectionEventFileMappingMapper inspectionEventFileMappingMapper;

  private String wayDepartmentName = "路桥巡检组";
  private String wayEventDepartmentName = "路桥抢修组";
  private String manholeDepartment = "排水抢修组";
  private String moduleName = "municipal_way";

  // 查询任务
  @Override
  public IPage<InspectionTasksVo> selectInspectionRecordManageVo(
      Page<InspectionTasksVo> inspectionRecordQueryVoPage, String status) {
    if (Strings.isBlank(status)) {
      return null;
    }
    if (status.equals("pc")) {
      Page<InspectionTasksVo> inspectionTasksVoPage =
          inspectionRecordMapper.selectInspectionRecordManageVoPage(
              inspectionRecordQueryVoPage, "新任务", "进行中");
      List<InspectionTasksVo> records = inspectionRecordQueryVoPage.getRecords();

      for (InspectionTasksVo record : records) {
        List<String> fileList = fileMapper.selectByIid(record.getId());
        record.setLink(fileList);
      }
      return inspectionTasksVoPage;
    } else {
      Page<InspectionTasksVo> inspectionTasksVoPage =
          inspectionRecordMapper.selectInspectionRecordManageVoPage(
              inspectionRecordQueryVoPage, status, null);
      List<InspectionTasksVo> records = inspectionRecordQueryVoPage.getRecords();
      for (InspectionTasksVo record : records) {
        List<String> fileList = fileMapper.selectByIid(record.getId());
        record.setLink(fileList);
      }
      return inspectionTasksVoPage;
    }
  }

  // 添加任务
  @Override
  public List<Long> insertInspectionRecordManage(InspectionRecordTaskDto dto) {
    // 实体类映射
    InspectionTasks inspectionTasks = inspectionDtoService.dtoMapByInspectionTasks(dto);
    List<InspectionRecord> inspectionRecords = inspectionDtoService.dtoMapByInspectionRecord(dto);
    List<Long> longs = new ArrayList<>();

    for (InspectionRecord inspectionRecord : inspectionRecords) {
      inspectionRecord.setId(null);
      String targetId = inspectionRecord.getTargetId();
      QueryWrapper<BridgeGis> bridgeGisQueryWrapper = new QueryWrapper<>();
      bridgeGisQueryWrapper.eq("code", targetId);
      BridgeGis bridgeGis = bridgeGisMapper.selectOne(bridgeGisQueryWrapper);
      if (bridgeGis != null) {
        inspectionTasks.setType("桥梁");
      }
      QueryWrapper<RoadGis> roadGisQueryWrapper = new QueryWrapper<>();
      QueryWrapper<RoadGis> code = roadGisQueryWrapper.eq("code", targetId);
      RoadGis roadGis = roadGisMapper.selectOne(code);
      if (roadGis != null) {
        inspectionTasks.setType("道路");
      }
      inspectionTasks.setStatus("新任务");
      // 设置周期执行时间
      inspectionTasks.setCycleExecuteTime(timeFormat(System.currentTimeMillis()));
      // 新建巡检记录
      if (inspectionTasksMapper.insert(inspectionTasks) > 0) {
        inspectionRecord.setInspectionTasksId(inspectionTasks.getId());
        // 新建巡检任务
        if (inspectionRecordMapper.insert(inspectionRecord) > 0) {
          longs.add(inspectionRecord.getId());
        }
      }
    }
    // 发送任务短信
    caseService.insertMessageToDepartment(
        ConstantUtil.MODULE_NAME.WAY, "巡检任务", "领取任务", wayDepartmentName);
    return longs;
  }

  // 批量删除
  @Override
  public boolean deleteInspectionRecordManage(List<Long> idList) {
    if (idList == null) {
      return false;
    }
    for (Long id : idList) {
      InspectionRecord inspectionRecord = inspectionRecordMapper.selectById(id);
      Long inspectionTasksId = inspectionRecord.getInspectionTasksId();
      InspectionTasks inspectionTasks = inspectionTasksMapper.selectById(inspectionTasksId);
      inspectionTasks.setStatus("任务已删除");
      // 删除文件关联表
      QueryWrapper<InspectionRecordFileMapping> wrapper = new QueryWrapper<>();
      QueryWrapper<InspectionRecordFileMapping> inspection_record_id =
          wrapper.eq("inspection_record_id", id);
      List<InspectionRecordFileMapping> InspectionRecordFileMappings =
          inspectionRecordFileMappingMapper.selectList(inspection_record_id);
      inspectionRecordFileMappingMapper.delete(inspection_record_id);
      for (InspectionRecordFileMapping inspectionRecordFileMapping : InspectionRecordFileMappings) {
        Long fileId = inspectionRecordFileMapping.getFileId();
        File file = fileMapper.selectById(fileId);
        String link = file.getLink();
        fileMapper.deleteById(fileId);
        try {
          FileUtil.removeFile(link);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      // 删除文件
      inspectionRecordMapper.deleteById(id);
    }
    return true;
  }

  // 提交任务并创建待完成记录
  @Override
  public int commitInspectionRecordManage(Long id) {
    // 实体类映射
    InspectionRecord inspectionRecord = inspectionRecordMapper.selectById(id);
    InspectionTasks inspectionTasks = inspectionRecordMapper.selectInspectionTasksIdById(id);
    inspectionTasks.setStatus("已完成");
    // 这里根据本次记录查询查询有几个案件
    Integer count = inspectionRecordQueryMapper.findIdByCount(inspectionRecord.getId());
    inspectionRecord.setResult("本次巡检发现" + count + "案件");
    inspectionRecord.setUpdateTime(timeFormat(System.currentTimeMillis()));
    inspectionTasks.setReportTime(timeFormat(System.currentTimeMillis()));
    if (inspectionRecordMapper.updateById(inspectionRecord) > 0) {
      // 判断下次执行时间是否为空 如果为空 则是即时任务 如果不为空则设置下次即时任务执行时间
      Timestamp timestamp = setNextTaskExecuteTime(inspectionTasks);
      if (Objects.nonNull(timestamp)) {
        inspectionTasks.setImmediatelyExecuteTime(timestamp);
        if (inspectionTasksMapper.updateById(inspectionTasks) > 0) {
          // 新建下次执行任务
          InspectionRecord newInspectionRecord = new InspectionRecord();
          newInspectionRecord.setTargetId(inspectionRecord.getTargetId());
          newInspectionRecord.setInspectionTasksId(inspectionTasks.getId());
          return inspectionRecordMapper.insert(newInspectionRecord);
        }
      }
      return 1;
    }
    return 0;
  }
  // 更新巡检记录
  @Override
  public int updateInspectionRecordManage(InspectionRecordTaskDto dto) {
    // 实体类映射
    InspectionTasks updateInspectionTasks = inspectionDtoService.dtoMapByInspectionTasks(dto);
    List<InspectionRecord> inspectionRecords = inspectionDtoService.dtoMapByInspectionRecord(dto);
    String[] split = getUrlsByRequest(dto.getLinkList());
    if (split != null && split.length > 0) {
      // 删除文件
      for (String s : split) {
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("link", s);
        File file = fileMapper.selectOne(fileQueryWrapper);
        if (file != null) {
          QueryWrapper<InspectionRecordFileMapping> fileMappingQueryWrapper = new QueryWrapper<>();
          fileMappingQueryWrapper.eq("file_id", file.getId());
          if (inspectionRecordFileMappingMapper.delete(fileMappingQueryWrapper) > 0) {
            fileMapper.deleteById(file.getId());
            try {
              FileUtil.removeFile(s);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
    for (InspectionRecord inspectionRecord : inspectionRecords) {
      InspectionTasks inspectionTasks =
          inspectionRecordMapper.selectInspectionTasksIdById(dto.getId());
      Long aLong = inspectionTasks.getId();
      updateInspectionTasks.setId(aLong);
      if (!updateInspectionTasks
              .getInspectionCycleType()
              .equals(inspectionTasks.getInspectionCycleType())
          || !updateInspectionTasks
              .getInspectionCycleNumber()
              .equals(inspectionTasks.getInspectionCycleNumber())) {
        // 如果为新任务继续发短信
        updateInspectionTasks.setStatus("新任务");
      }
      // 更新巡检任务
      if (inspectionTasksMapper.updateById(updateInspectionTasks) > 0) {
        // 更新巡检记录
        inspectionRecordMapper.updateById(inspectionRecord);
      }
    }
    // 发送任务短信
    caseService.insertMessageToDepartment(
        ConstantUtil.MODULE_NAME.WAY, "巡检任务更新", "领取任务", wayDepartmentName);
    return 1;
  }

  // 接单时间
  @Override
  public int receivingTaskInspection(Long id) {
    InspectionRecord inspectionRecord = inspectionRecordMapper.selectById(id);
    InspectionTasks inspectionTasks = new InspectionTasks();
    inspectionTasks.setId(inspectionRecord.getInspectionTasksId());
    // 逻辑不通改
    inspectionTasks.setStatus("进行中");
    inspectionTasks.setImmediatelyExecuteTime(timeFormat(System.currentTimeMillis()));
    if (inspectionRecordMapper.updateById(inspectionRecord) > 0) {
      if (inspectionTasksMapper.updateById(inspectionTasks) > 0) {
        return 1;
      }
    }
    return 0;
  }

  @Override
  public List<TaskVo> selectTaskAll(String status) {
    String token = request.getHeader(HEADER_STRING);
    Claims claims = JwtUtil.parseJWT(token);
    Long id = Long.valueOf(String.valueOf(claims.get("id")));
    List<String> departmentList = departmentMapper.selectDepartmentListBySubjectId(id);
    List<TaskVo> taskVos = new ArrayList<>();
    for (String department : departmentList) {
      if (department.equals(wayDepartmentName)) {
        List<InspectionTasksVo> inspectionTasksVos =
            inspectionRecordMapper.selectInspectionRecordManageVo();
        selectInspectionStatus(inspectionTasksVos, status);
        // 巡检任务批量插入
        for (InspectionTasksVo inspectionTasksVo : inspectionTasksVos) {
          TaskVo taskVo = new TaskVo();
          taskVo.setId(inspectionTasksVo.getId());
          //获取link地址
          QueryWrapper<InspectionRecordFileMapping> queryWrapper = new QueryWrapper<>();
          queryWrapper.eq("inspection_record_id",inspectionTasksVo.getId());
            List<InspectionRecordFileMapping> inspectionRecordFileMappings = inspectionRecordFileMappingMapper.selectList(queryWrapper);
           if(Objects.nonNull(inspectionRecordFileMappings)&&!inspectionRecordFileMappings.isEmpty()){
               File file = fileMapper.selectById(inspectionRecordFileMappings.get(0).getFileId());
               if(Objects.nonNull(file)){
                   taskVo.setLink(file.getLink());
               }
           }
          taskVo.setModuleName("一路一档系统");
          taskVo.setType("巡检任务");
          taskVo.setCreateTime(inspectionTasksVo.getCreateTime());
          taskVos.add(taskVo);
        }
      } else if (department.equals(wayEventDepartmentName)) {
        // 新任务
        Page<OneWayCaseNoHandVo> page = new Page<>();
        List<OneWayCaseNoHandVo> all = caseManagementService.selectInspectionNewTest(caseType);
        if (status.equals("新任务")) {
          for (OneWayCaseNoHandVo oneWayCaseNoHandVo : all) {
            TaskVo taskVo = new TaskVo();
            taskVo.setId(oneWayCaseNoHandVo.getCid());
            Case aCase = caseMapper.getById(oneWayCaseNoHandVo.getCid());
            taskVo.setModuleName(aCase.getType());
            taskVo.setLink(oneWayCaseNoHandVo.getLink());
            taskVo.setType("即时任务");
            taskVo.setCreateTime(oneWayCaseNoHandVo.getCreateTime());
            taskVos.add(taskVo);
          }
        }
        // 进行中
        Page<OneWayCaseNoHandVo> oneWayCaseNoHandVoPage =
            caseManagementService.selectInspectionCaseOnlyHandingPage(page, caseType);
        List<OneWayCaseNoHandVo> records = oneWayCaseNoHandVoPage.getRecords();
        if (status.equals("进行中")) {
          for (OneWayCaseNoHandVo record : records) {
            TaskVo taskVo = new TaskVo();
            taskVo.setId(record.getCid());
            taskVo.setLink(record.getLink());
            taskVo.setType("即时任务");
            Case aCase = caseMapper.getById(record.getCid());
            taskVo.setModuleName(aCase.getType());
            taskVo.setCreateTime(record.getCreateTime());
            taskVos.add(taskVo);
          }
        }

        // 已处理
        Page<OneWayCaseNoHandVo> inspectionCaseIsHandledPage =
            caseManagementService.selectInspectionCaseIsHandledPage(
                page, caseType, null, null, null, null, null);
        List<OneWayCaseNoHandVo> inspectionCaseIsHandledPageRecords =
            inspectionCaseIsHandledPage.getRecords();
        for (OneWayCaseNoHandVo handVo : inspectionCaseIsHandledPageRecords) {
          TaskVo taskVo = new TaskVo();
          taskVo.setId(handVo.getCid());
          taskVo.setLink(handVo.getLink());
          taskVo.setType("即时任务");
          Case aCase = caseMapper.getById(handVo.getCid());
          taskVo.setModuleName(aCase.getType());
          taskVo.setCreateTime(handVo.getCreateTime());
          taskVos.add(taskVo);
        }
      }else if(department.equals(manholeDepartment)){
        List<OneWayCaseNoHandVo> otherTasks = caseMapper.selectOtherTask(status);
        caseManagementService.formatOneWayCaseNoHandVo(otherTasks);
        for (OneWayCaseNoHandVo handVo : otherTasks) {
          TaskVo taskVo = new TaskVo();
          taskVo.setId(handVo.getCid());
          taskVo.setLink(handVo.getLink());
          taskVo.setType("即时任务");
          Case aCase = caseMapper.getById(handVo.getCid());
          taskVo.setModuleName(aCase.getType());
          taskVo.setCreateTime(handVo.getCreateTime());
          taskVos.add(taskVo);
        }
      }
    }
    return taskVos;
  }

  @Override
  public int insertInspectionCase(NewCase newCase) {
    //        String token = request.getHeader(HEADER_STRING);
    //        Claims claims = JwtUtil.parseJWT(token);
    //        Long id = Long.valueOf(String.valueOf(claims.get("id")));
    //        UserInfo userInfo = userInfoMapper.selectUserInfoIdBySubject(id);
    // 新建案件
    Case c = new Case();
    // 前台获取
    c.setGrade(newCase.getGrade());
    c.setCaseDescribe(newCase.getCaseDescribe());
    c.setAdvice(newCase.getAdvice());
    // c.setCaseType(newCase.getCaseType());
    // 后台默认
    c.setType("巡检案件");
    c.setStatus("未处理");

    c.setCreateTime(timeFormat(System.currentTimeMillis()));
    c.setUpdateTime(timeFormat(System.currentTimeMillis()));

    // 新建巡检事件
    InspectionEvent inspectionEvent = new InspectionEvent();
    // 前台获取
    if (newCase.getCreateTime() == null) {
      inspectionEvent.setCreateTime(timeFormat(System.currentTimeMillis()));
    } else {
      inspectionEvent.setCreateTime(newCase.getCreateTime());
    }
    // 发送任务短信
    caseService.insertMessageToDepartment(
        ConstantUtil.MODULE_NAME.WAY, "案件处理任务", "领取任务", wayDepartmentName);
    inspectionEvent.setReason(newCase.getReason());
    inspectionEvent.setReportUserInfoId(newCase.getReportUserInfoId());
    inspectionEvent.setSendOrder(true);
    inspectionEvent.setRemark(newCase.getRemark());
    inspectionEvent.setLevel(newCase.getGrade());
    inspectionEvent.setFacilityId(newCase.getLocation());
    // 后台默认
    inspectionEvent.setReset("否");
    inspectionEvent.setDataOrigin("移动端上报");
    // todo 类型判断待确定
    inspectionEvent.setType("巡检事件");
    inspectionEvent.setCreateTime(timeFormat(System.currentTimeMillis()));
    inspectionEvent.setUpdateTime(timeFormat(System.currentTimeMillis()));
    Long cid = null;
    Long iid = null;
    if (caseMapper.insertCase(c) > 0) {
      cid = c.getId();
    }
    if (inspectionEventMapper.insert(inspectionEvent) > 0) {
      iid = inspectionEvent.getId();
    }
    // 新建巡检事件与案件关联
    CaseInspectionEventMapping caseInspectionEventMapping = new CaseInspectionEventMapping();
    caseInspectionEventMapping.setCaseId(cid);
    caseInspectionEventMapping.setInspectionEventId(iid);
    caseInspectionEventMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
    caseInspectionEventMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
    caseInspectionEventMappingMapper.insert(caseInspectionEventMapping);
    // 处理前台传出的文件
    if (newCase.getFile() != null) {
      File file = null;
      try {
        file = getFilePo(null, moduleName, newCase.getFile());
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (fileMapper.insert(file) > 0) {
        Long fid = file.getId();
        // 关联巡检时间与文件关联
        InspectionEventFileMapping inspectionEventFileMapping = new InspectionEventFileMapping();
        inspectionEventFileMapping.setFileId(fid);
        inspectionEventFileMapping.setInspectionEventId(iid);
        inspectionEventFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
        inspectionEventFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
        return inspectionEventFileMappingMapper.insert(inspectionEventFileMapping);
      }
    }
    return 0;
  }

  @Override
  public int updateInspectionFile(UpdateFileDto updateFileDto) {
    MultipartFile multipartFile = updateFileDto.getFile();
    List<Long> longs = updateFileDto.getIds();
    if (multipartFile == null || longs.size() == 0) {
      return 0;
    }
    File file = null;
    for (Long id : longs) {
      if (Objects.isNull(inspectionRecordMapper.selectById(id))) {
        continue;
      }
      if (Objects.isNull(file)) {
        file = new File();
        if (fileDuplicateCode.insertFileDuplicateCode(file, multipartFile, WAY_MODULE) > 0) {
          InspectionRecordFileMapping inspectionRecordFileMapping =
              new InspectionRecordFileMapping();
          inspectionRecordFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
          inspectionRecordFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
          inspectionRecordFileMapping.setInspectionRecordId(id);
          inspectionRecordFileMapping.setFileId(file.getId());
          inspectionRecordFileMappingMapper.insert(inspectionRecordFileMapping);
        }
      } else {
        InspectionRecordFileMapping inspectionRecordFileMapping = new InspectionRecordFileMapping();
        inspectionRecordFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
        inspectionRecordFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
        inspectionRecordFileMapping.setInspectionRecordId(id);
        inspectionRecordFileMapping.setFileId(file.getId());
        inspectionRecordFileMappingMapper.insert(inspectionRecordFileMapping);
      }
    }
    return 1;
  }

  // 判断下次任务执行时间
  private Timestamp setNextTaskExecuteTime(InspectionTasks inspectionTasks) {
    Timestamp cycleExecuteTime = inspectionTasks.getCycleExecuteTime();
    Long inspectionCycleNumber = inspectionTasks.getInspectionCycleNumber();
    String inspectionCycleType = inspectionTasks.getInspectionCycleType();
    // 判断下次执行时间是否已经大于当前时间
    if (cycleExecuteTime.after(DateFormatUtil.timeFormat(System.currentTimeMillis()))) {
      return null;
    }
    switch (inspectionCycleType) {
      case "天":
        return new Timestamp(cycleExecuteTime.getTime() + 60 * 60 * 24 * inspectionCycleNumber);
      case "周":
        return new Timestamp(cycleExecuteTime.getTime() + 60 * 60 * 24 * 7 * inspectionCycleNumber);
      case "月":
        return new Timestamp(
            cycleExecuteTime.getTime() + 60 * 60 * 24 * 30 * inspectionCycleNumber);
    }
    return null;
  }

  // 查询
  private void selectInspectionStatus(List<InspectionTasksVo> records, String status) {
    Iterator<InspectionTasksVo> iterator = records.iterator();
    while (iterator.hasNext()) {
      InspectionTasksVo record = iterator.next();
      if (status.equals(record.getStatus())
          || ((status.equals("新任务") || status.equals("已完成")))) {
        forRecord(record);
      }
      if (status.equals("pc")) {
        if (record.getStatus().equals("进行中")) {
          forRecord(record);
        } else if (record.getStatus().equals("新任务")) {
          forRecord(record);
        }
      }
      if (record.getTargetName() == null) {
        iterator.remove();
      }
    }
  }

  // 遍历查询封装
  private void forRecord(InspectionTasksVo record) {
    if (record.getType().equals("道路")) {
      QueryWrapper<RoadGis> roadGisQueryWrapper = new QueryWrapper<>();
      roadGisQueryWrapper.eq("code", record.getTargetId());
      RoadGis roadGis = roadGisMapper.selectOne(roadGisQueryWrapper);
      if (roadGis.getName() != null) {
        record.setTargetName(roadGis.getName());
      }
    } else if (record.getType().equals("桥梁")) {
      QueryWrapper<BridgeGis> bridgeGisQueryWrapper = new QueryWrapper<>();
      bridgeGisQueryWrapper.eq("code", record.getTargetId());
      BridgeGis bridgeGis = bridgeGisMapper.selectOne(bridgeGisQueryWrapper);
      if (bridgeGis.getName() != null) {
        record.setTargetName(bridgeGis.getName());
      }
    }
  }
}
